<?php
/**
* 
*/
class UserFollowsController extends AppController
{
	var $name = 'UserFollows';
	var $uses = array('UserFollow','User','Board');
	var $helpers = array('Html','Session');
	var $components = array('Session');

	public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('viewAllBoards');
    }

    public function viewAllBoards(){
        $userId = 0;
    	if( $userId = $this->Auth->user('id')){
        }
        //lấy các board userid đang theo dõi
    	$boardFolloweds = $this->UserFollow->find('all',array(
    														'conditions'=>array(
    															'UserFollow.user_id'=>$userId),
    														'order'=>array(
    															'UserFollow.created'=>'desc')));
        //lấy các board còn lại, theo thứ tự created
        $boards = $this->Board->find('all',array(/*
                                              'conditions'=>array(
                                                    'not'=>array(
                                                        'UserFollow..user_id'=>$userId)),*/
                                             'order'=>array(
                                                  'Board.created'=>'desc')));
        $this->set('boardFolloweds', $boardFolloweds);
        $this->set('boards', $boards);
    }

    public function followBoard($boardId = null){
    	if($boardId){
            debug($boardId);
    		$userFollowBoard['UserFollow'] = array(
    									'user_id'=>$this->Auth->user('id'),
    									'board_id'=>$boardId,
    									'create'=>date('m/d/Y h:i:s a', time()));
    		debug($userFollowBoard);

    		$this->UserFollow->create();
    		if( $this->UserFollow->save($userFollowBoard) ){
    			//$this->Session->setFlash(__('followed success'));
    			return $this->redirect( array('controller'=>'Boards','action'=>'viewInfoBoards'));
    		}
    	}

    	$this->Session->setFlash(__('error follow'));
    }

    public function unfollowBoard($boardId = null){
    	debug($boardId);
    	if($boardId){
            $userFollow = $this->UserFollow->findByBoard_id($boardId);
    		if( $this->UserFollow->delete($userFollow['UserFollow']['id'])){
    			//$this->Session->setFlash(__('unfollowed success'));
    			return $this->redirect( array('controller'=>'Boards','action'=>'viewInfoBoards'));
    		}
    	}

    	$this->Session->setFlash(__('error unfollow'));
    }
}
?>